<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>AVR Variable Attributes (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="AVR Variable Attributes (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="AVR Variable Attributes (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Variable-Attributes.html#Variable-Attributes" rel="up" title="Variable Attributes">
<link href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" rel="next" title="Blackfin Variable Attributes">
<link href="ARC-Variable-Attributes.html#ARC-Variable-Attributes" rel="prev" title="ARC Variable Attributes">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en">
<a name="AVR-Variable-Attributes"></a>
<div class="header">
<p>
Next: <a href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" accesskey="n" rel="next">Blackfin Variable Attributes</a>, Previous: <a href="ARC-Variable-Attributes.html#ARC-Variable-Attributes" accesskey="p" rel="prev">ARC Variable Attributes</a>, Up: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="u" rel="up">Variable Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="AVR-Variable-Attributes-1"></a>
<h4 class="subsection">6.34.3 AVR Variable Attributes</h4>

<dl compact="compact">
<dt><code>progmem</code></dt>
<dd><a name="index-progmem-variable-attribute_002c-AVR"></a>
<p>The <code>progmem</code> attribute is used on the AVR to place read-only
data in the non-volatile program memory (flash). The <code>progmem</code>
attribute accomplishes this by putting respective variables into a
section whose name starts with <code>.progmem</code>.
</p>
<p>This attribute works similar to the <code>section</code> attribute
but adds additional checking.
</p>
<dl compact="compact">
<dt>&bull;&nbsp; Ordinary AVR cores with 32 general purpose registers:</dt>
<dd><p><code>progmem</code> affects the location
of the data but not how this data is accessed.
In order to read data located with the <code>progmem</code> attribute
(inline) assembler must be used.
</p><div class="smallexample">
<pre class="smallexample">/* Use custom macros from <a href="http://nongnu.org/avr-libc/user-manual/">AVR-LibC</a><!-- /@w --> */
#include &lt;avr/pgmspace.h&gt; 

/* Locate var in flash memory */
const int var[2] PROGMEM = { 1, 2 };

int read_var (int i)
{
    /* Access var[] by accessor macro from avr/pgmspace.h */
    return (int) pgm_read_word (&amp; var[i]);
}
</pre></div>

<p>AVR is a Harvard architecture processor and data and read-only data
normally resides in the data memory (RAM).
</p>
<p>See also the <a href="Named-Address-Spaces.html#AVR-Named-Address-Spaces">AVR Named Address Spaces</a> section for
an alternate way to locate and access data in flash memory.
</p>
</dd>
<dt>&bull;&nbsp; AVR cores with flash memory visible in the RAM address range:</dt>
<dd><p>On such devices, there is no need for attribute <code>progmem</code> or
<a href="Named-Address-Spaces.html#AVR-Named-Address-Spaces"><code>__flash</code></a> qualifier at all.
Just use standard C / C++.  The compiler will generate <code>LD*</code>
instructions.  As flash memory is visible in the RAM address range,
and the default linker script does <em>not</em> locate <code>.rodata</code> in
RAM, no special features are needed in order not to waste RAM for
read-only data or to read from flash.  You might even get slightly better
performance by
avoiding <code>progmem</code> and <code>__flash</code>.  This applies to devices from
families <code>avrtiny</code> and <code>avrxmega3</code>, see <a href="AVR-Options.html#AVR-Options">AVR Options</a> for
an overview.
</p>
</dd>
<dt>&bull;&nbsp;Reduced AVR Tiny cores like ATtiny40:</dt>
<dd><p>The compiler adds <code>0x4000</code>
to the addresses of objects and declarations in <code>progmem</code> and locates
the objects in flash memory, namely in section <code>.progmem.data</code>.
The offset is needed because the flash memory is visible in the RAM
address space starting at address <code>0x4000</code>.
</p>
<p>Data in <code>progmem</code> can be accessed by means of ordinary C&nbsp;code,
no special functions or macros are needed.
</p>
<div class="smallexample">
<pre class="smallexample">/* var is located in flash memory */
extern const int var[2] __attribute__((progmem));

int read_var (int i)
{
    return var[i];
}
</pre></div>

<p>Please notice that on these devices, there is no need for <code>progmem</code>
at all.
</p>
</dd>
</dl>

</dd>
<dt><code>io</code></dt>
<dt><code>io (<var>addr</var>)</code></dt>
<dd><a name="index-io-variable-attribute_002c-AVR"></a>
<p>Variables with the <code>io</code> attribute are used to address
memory-mapped peripherals in the io address range.
If an address is specified, the variable
is assigned that address, and the value is interpreted as an
address in the data address space.
Example:
</p>
<div class="smallexample">
<pre class="smallexample">volatile int porta __attribute__((io (0x22)));
</pre></div>

<p>The address specified in the address in the data address range.
</p>
<p>Otherwise, the variable it is not assigned an address, but the
compiler will still use in/out instructions where applicable,
assuming some other module assigns an address in the io address range.
Example:
</p>
<div class="smallexample">
<pre class="smallexample">extern volatile int porta __attribute__((io));
</pre></div>

</dd>
<dt><code>io_low</code></dt>
<dt><code>io_low (<var>addr</var>)</code></dt>
<dd><a name="index-io_005flow-variable-attribute_002c-AVR"></a>
<p>This is like the <code>io</code> attribute, but additionally it informs the
compiler that the object lies in the lower half of the I/O area,
allowing the use of <code>cbi</code>, <code>sbi</code>, <code>sbic</code> and <code>sbis</code>
instructions.
</p>
</dd>
<dt><code>address</code></dt>
<dt><code>address (<var>addr</var>)</code></dt>
<dd><a name="index-address-variable-attribute_002c-AVR"></a>
<p>Variables with the <code>address</code> attribute are used to address
memory-mapped peripherals that may lie outside the io address range.
</p>
<div class="smallexample">
<pre class="smallexample">volatile int porta __attribute__((address (0x600)));
</pre></div>

</dd>
<dt><code>absdata</code></dt>
<dd><a name="index-absdata-variable-attribute_002c-AVR"></a>
<p>Variables in static storage and with the <code>absdata</code> attribute can
be accessed by the <code>LDS</code> and <code>STS</code> instructions which take
absolute addresses.
</p>
<ul>
<li> This attribute is only supported for the reduced AVR Tiny core
like ATtiny40.

</li><li> You must make sure that respective data is located in the
address range <code>0x40</code>&hellip;<code>0xbf</code> accessible by
<code>LDS</code> and <code>STS</code>.  One way to achieve this as an
appropriate linker description file.

</li><li> If the location does not fit the address range of <code>LDS</code>
and <code>STS</code>, there is currently (Binutils 2.26) just an unspecific
warning like
<blockquote>
<p><code>module.c:(.text+0x1c): warning: internal error: out of range error</code>
</p></blockquote>

</li></ul>

<p>See also the <samp>-mabsdata</samp> <a href="AVR-Options.html#AVR-Options">command-line option</a>.
</p>
</dd>
</dl>

<hr>
<div class="header">
<p>
Next: <a href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" accesskey="n" rel="next">Blackfin Variable Attributes</a>, Previous: <a href="ARC-Variable-Attributes.html#ARC-Variable-Attributes" accesskey="p" rel="prev">ARC Variable Attributes</a>, Up: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="u" rel="up">Variable Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
